﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Services;

public partial class PermintaanBarangUser : System.Web.UI.Page
{
    private static string strConn = ConfigurationManager.ConnectionStrings["LabConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        populateBarang();
        populateTahunAnggaran();
    }

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
    public static DatabaseResponse Permintaan(int tahun, int barang, double jumlah, string keterangan, int user)
    {
        DatabaseResponse result = new DatabaseResponse();
        try
        {
            using (SqlConnection cn = new SqlConnection(strConn))
            {
                cn.Open();
                string query = "sp_barang_permintaan_user {0}, {1}, {2}, {3}, '{4}', {5}";
                using (SqlCommand cmd = new SqlCommand(string.Format(query, barang, tahun, jumlah, user, keterangan, user)))
                {
                    cmd.Connection = cn;
                    int ra = cmd.ExecuteNonQuery();
                    if (ra > 0)
                    {
                        result.Status = true;
                    }
                    else
                    {
                        result.Status = false;
                        result.ErrorMessage = "Tidak ada data yang berhasil ditulis.";
                    }
                }
                cn.Close();
            }
        }
        catch (Exception ex)
        {

            result.Status = false;
            result.ErrorMessage = ex.Message;
        }
        return result;
    }

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
    public static List<ListPermintaanUser> ListPermintaan(int tahun, int user)
    {
        List<ListPermintaanUser> result = new List<ListPermintaanUser>();
        DataTable dt = new DataTable();
        string query = "sp_permintaan_list_per_user {0}, {1}";
        using (SqlDataAdapter da = new SqlDataAdapter(string.Format(query, tahun, user), strConn))
        {
            da.Fill(dt);
        }

        foreach (DataRow item in dt.Rows)
        {
            result.Add(new ListPermintaanUser()
            {
                NamaBarang = item["NAMA_BARANG"].ToString(),
                Jumlah = double.Parse(item["JUMLAH"].ToString()),
                Satuan = item["SATUAN"].ToString(),
                Keterangan = item["SATUAN"].ToString()
            });
        }
        return result;
    }

    private void populateTahunAnggaran()
    {
        string query = "sp_get_tahun_anggaran";
        DataTable dt = new DataTable();
        using (SqlDataAdapter da = new SqlDataAdapter(query, strConn))
        {
            da.Fill(dt);
        }
        DataTable tahun = new DataTable();
        tahun.Columns.Add("TAHUN");
        if (dt.Rows.Count > 0)
        {
            int min = int.Parse(dt.Rows[0]["TAHUN1"].ToString());
            int max = int.Parse(dt.Rows[0]["TAHUN2"].ToString());
            for (int i = min; i <= max; i++)
            {
                DataRow dr = tahun.NewRow();
                dr["TAHUN"] = i;
                tahun.Rows.Add(dr);
            }
        }
        lstTahunAnggaran.DataSource = tahun;
        lstTahunAnggaran.DataBind();

        lstTahunAnggaran2.DataSource = tahun;
        lstTahunAnggaran2.DataBind();
    }

    private void populateBarang()
    {
        string query = "sp_namabarang_list";
        DataTable dt = new DataTable();
        using (SqlDataAdapter da = new SqlDataAdapter(query, strConn))
        {
            da.Fill(dt);
        }
        lstBarang.DataSource = dt;
        lstBarang.DataBind();
    }

    public class DatabaseResponse
    {
        public bool Status;
        public string ErrorMessage;
        public string Value;
    }

    public class ListPermintaanUser
    {
        public string NamaBarang;
        public double Jumlah;
        public string Satuan;
        public string Keterangan;
    }
}